package pCloud.message;

import java.io.Serializable;
import java.util.ArrayList;

import pCloud.message.StreamServerReply;

import com.sun.xacml.ctx.Result;

/**
 * Represent the reply from data server to proxy and from proxy to client. This
 * include the AC decision and a DatabaseReply object containing the requested
 * data.
 * 
 */
public class ReplyMessage implements Serializable {
	int requestId;
	int decision;
	DatabaseReply data;
	StreamServerReply streamMessage;
	ArrayList<String> matchingPolicyIds=null;
	
	public ReplyMessage(int requestId,int d){
		this.requestId = requestId;
		this.decision = d;
		this.streamMessage = null;
		this.data = null;
		this.matchingPolicyIds = new ArrayList<String>();
		
	}
	
	public ReplyMessage(int requestId,int d,StreamServerReply sr){
		this.requestId = requestId;
		this.decision = d;
		this.streamMessage = sr;
		this.data = null;
		this.matchingPolicyIds = new ArrayList<String>();
		
	}
	
	public ReplyMessage(int requestId, int d, DatabaseReply data) {
		this.requestId = requestId; 
		this.decision = d;
		this.data = data;
		this.matchingPolicyIds = new ArrayList<String>();
	}
	

	public int getRequestId(){
		return this.requestId;
	}

	public void setMatchingPolicyIds(ArrayList<String> mpi){
		this.matchingPolicyIds = mpi;
	}
	
	public ArrayList<String> getMatchingPolicyIds(){
		return this.matchingPolicyIds;
	}
	
	public void addMatchingPolicyIds(ArrayList<String> mpi){
		for (int i=0; i<mpi.size(); i++)
			this.matchingPolicyIds.add(mpi.get(i));
	}
	
	public int getDecision() {
		return this.decision;
	}

	public DatabaseReply getDatabaseReply() {
		return this.data;
	}

	public void setDatabaseReply(DatabaseReply dbr) {
		this.data = dbr;
	}

	public StreamServerReply getStreamServerReply(){
		return this.streamMessage;
	}
	
	public void setStreamServerReply(StreamServerReply ssr){
		this.streamMessage = ssr;
	}
	
	public String toString() {
		String dec = "";
		if (this.decision == Result.DECISION_PERMIT)
			dec = "Permit";
		else if (this.decision == Result.DECISION_DENY)
			dec = "Deny";
		else if (this.decision == Result.DECISION_INDETERMINATE)
			dec = "Intermediate";
		else if (this.decision == Result.DECISION_NOT_APPLICABLE)
			dec = "Not Applicable";
		
		String matchingPolicies = "";
		if (this.matchingPolicyIds==null || this.matchingPolicyIds.size()==0)
			matchingPolicies = "No matching policy";
		else
		{
			matchingPolicies = "Matching policies: ";
			for (int i=0; i<this.matchingPolicyIds.size(); i++)
				matchingPolicies += this.matchingPolicyIds.get(i)+"\t";
		}
		if (this.streamMessage != null){
			return matchingPolicies+"\n Decision = "+dec+"\n"
					+ "StreamMessage :\n" + this.streamMessage;
		}
		return matchingPolicies+"\n Decision = "+dec+"\n"
			+ "Data: \n"+this.data;
	}
}
